<template>
  <div v-show="editShow">
    <div class="container">
      <el-row :gutter="20">
        <div class="clearfix">
          <div class="crumbs">
            <i class="el-icon-back"></i>
            <el-button type="text" @click="goBack">返回</el-button>
          </div>
          <div class="btn-save">
            <el-button type="primary" @click="saveClick('ruleForm')">保存</el-button>
          </div>
        </div>
        <div class="form-box">
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="120px"
            class="eidt-el-form"
          >
            <el-form-item label="人脸识别" prop="faceMatch">
              <el-select
                v-model="ruleForm.faceMatch"
                placeholder="选择人脸识别"
                @change="selectFaceMatch"
              >
                <el-option label="不人脸识别" value="0"></el-option>
                <el-option label="人证核验" value="1"></el-option>
                <el-option label="人脸识别" value="2"></el-option>
                <el-option label="人证核验或人脸识别" value="3"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="人脸采集" prop="faceCollect">
              <el-select
                v-model="ruleForm.faceCollect"
                placeholder="选择人脸采集"
                @change="selectFaceCollect"
              >
                <el-option label="不采集" value="0"></el-option>
                <el-option label="采集人脸后开门" value="1"></el-option>
                <el-option label="采集现场照片" value="2"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="证件类型">
              <el-select v-model="ruleForm.documentTypeId" placeholder="选择证件类型">
                <el-option
                  v-for="(type,index) in DocumentType"
                  :label="type.name"
                  :key="index"
                  :value="type.documentTypeId"
                >
                  <div @click="getChangeType(type)">{{type.name}}</div>
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="票类型">
              <el-select v-model="ruleForm.ticketTypeId" placeholder="选择证件类型">
                <el-option
                  v-for="(type,index) in TicketType"
                  :label="type.name"
                  :key="index"
                  :value="type.ticketTypeId"
                >
                  <div @click="getChangeTicketType(type)">{{type.name}}</div>
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="运行通行日期" prop="days">
              <el-date-picker
                v-model="ruleForm.days"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              ></el-date-picker>
            </el-form-item>
            <el-form-item label="允许通行次数" prop="frequency">
              <el-input v-model="ruleForm.frequency" type="number" min="1"></el-input>
            </el-form-item>
          </el-form>
        </div>
      </el-row>
    </div>
  </div>
</template>

<script>


export default {
  name: "editaccessmanage",
  data() {
    return {
      rules: {
        faceMatch: [
          { required: true, message: "请选择人脸识别", trigger: "blur" }
        ],
        faceCollect: [
          { required: true, message: "请选择人脸采集", trigger: "blur" }
        ],
        days: [{ required: true, message: "请填写通行日期", trigger: "blur" }]
      },
      ruleForm: {
        faceMatch: "",
        faceCollect: "",
        documentTypeId: "",
        ticketTypeId: "",
        days: "",
        frequency: "",
        name: ""
      },
      ticketCom_Id: "",
      dataList: [],
      ticketTypeId: "",
      DocumentType: [],
      TicketType: [],
      projectId: localStorage.getItem("project_Id")
    };
  },
  props: {
    editShow: {
      type: Boolean,
      default: false
    },
    editData: {
      type: Object
    }
  },
  created() { 

    this.getTicketType();
    this.getDocumentType();
  },
  watch: {
    editData: function() {
      const item = this.editData;
      console.log(item, 1231);
      this.ruleForm = {
        faceMatch: item.faceMatch,
        faceCollect: item.faceCollect,
        documentTypeId: item.documentTypeId,
        ticketTypeId: item.ticketTypeId,
        documentTypeId: item.documentTypeId,
        days: item.days,
        frequency: item.frequency
      };
      this.ticketCom_Id = item.accessRuleId;
    }
  },
  methods: {
    saveClick(ruleForm) {
      this.$refs[ruleForm].validate(valid => {
        if (valid) {
          if (
            this.ruleForm.documentTypeId != "" &&
            this.ruleForm.ticketTypeId != ""
          ) {
            this.$message.error("证件类型和票类型只能选择一种！");
          } else {
            this.ruleForm.days[0].getFullYear() +
              "-" +
              (this.ruleForm.days[0].getMonth() + 1);

            this.ruleForm = {
              faceMatch: this.ruleForm.faceMatch,
              faceCollect: this.ruleForm.faceCollect,
              documentTypeId: this.ruleForm.documentTypeId,
              ticketTypeId: this.ruleForm.ticketTypeId,
              days:
                this.getNowFormatDate(this.ruleForm.days[0]) +
                "," +
                this.getNowFormatDate(this.ruleForm.days[1]),
              frequency: this.ruleForm.frequency,
              name: this.ruleForm.name,
              projectId: this.projectId
            };

            if (this.ticketCom_Id == "" || this.ticketCom_Id == undefined) {
              this.$axios.post("/AccessRule/", this.ruleForm).then(res => {
                if (res.data.hasError == false) {
                  this.$message.success("保存成功！");
                  this.goBack();
                  this.$refs[ruleForm].resetFields();
                } else {
                  this.$message.error("保存失败！");
                }
              });
            } else {
              this.$axios
                .put("/AccessRule/" + this.ticketCom_Id + "", this.ruleForm)
                .then(res => {
                  if (res.data.hasError == false) {
                    this.$message.success("编辑成功！");
                    this.goBack();
                    this.$refs[ruleForm].resetFields();
                  } else {
                    this.$message.error("编辑失败！");
                  }
                });
            }
          }
        } else {
          return false;
        }
      });
    },
    getNowFormatDate(date) {
      var seperator1 = "-";
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      var strDate = date.getDate();
      if (month >= 1 && month <= 9) {
        month = "0" + month;
      }
      if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
      }
      var currentdate = year + seperator1 + month + seperator1 + strDate;
      return currentdate;
    },
    goBack() {
      this.$emit("backFunStu");
      this.ticketCom_Id = "";
    },
    getDocumentType() {
      this.$axios.get("/DocumentType/Project/"+this.projectId).then(res => {
        if (res.data.hasError == false) {
          this.DocumentType = [...res.data.data];
        }
      });
    },
    getTicketType() {
      this.$axios.get("/TicketType/Project/"+this.projectId).then(res => {
        if (res.data.hasError == false) {
          this.TicketType = [...res.data.data];
        }
      });
    },
    getChangeType(type) {
      this.ruleForm.documentTypeId = type.documentTypeId;
      this.ruleForm.ticketTypeId = "";
      this.ruleForm.name = type.name;
    },
    getChangeTicketType(type) {
      this.ruleForm.ticketTypeId = type.ticketTypeId;
      this.ruleForm.documentTypeId = "";
      this.ruleForm.name = type.name;
    },
    selectFaceCollect(value) {
      this.ruleForm.faceCollect = value;
    },
    selectFaceMatch(value) {
      this.ruleForm.faceMatch = value;
    }
  }
};
</script>